Skip to content

Fix MBASub pass order to prevent instruction explosion#2

Merged
PeterHackz merged 1 commit intoPeterHackz:masterfrom
metamizol:master
Feb 18, 2026
Merged

Fix MBASub pass order to prevent instruction explosion#2
PeterHackz merged 1 commit intoPeterHackz:masterfrom
metamizol:master

Conversation

@metamizol
Copy link
Contributor

I reordered the MBA transformation passes in MBASub::RunOnBasicBlock so that runOnMul runs first, before runOnSub, runOnAdd, and runOnXor.

The problem

Some of the transformations (especially in xor_ops and sub_ops) generate new Mul instructions as part of their MBA formulas.
For example:

From xor_ops:
a ^ b = (a + b) - 2 * (a & b)

From sub_ops:
X - Y == (X ^ -Y) + 2*(X & -Y)

Before runOnMul would run after these passes, immediately picking up these newly generated Mul instructions and expanding them again using mul_ops (which itself expands 1 Mul into 2 Muls + overhead).

The issue is that mul_ops expands a multiplication into multiple new multiplications:
b * c = (((b | c) * (b & c)) + ((b & ~c) * (c & ~b)))

So what ended up happening was:

Sub / Xor generate Mul, Mul expands into more Mul, Those can generate even more Mul and Repeat next iteration xD
On math-heavy or crypto-heavy code (like ChaCha20), this cause the IR size to explode and hanging at runtime

@PeterHackz
Copy link
Owner

Good catch! Thank you for pointing this out.

I’m considering removing or updating the SUB and XOR multiplication cases since they may be unsafe for signed arithmetic. Will revisit when I have more time.

@PeterHackz PeterHackz merged commit 1fc53ad into PeterHackz:master Feb 18, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants